package com.woniuxy.exam.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.dto.QuestionDto;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.TrueOrFalse;
import com.woniuxy.commons.utils.TimeUtils;
import com.woniuxy.exam.service.TrueOrFalseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zouyang
 * @date 2020/7/17 - 23:30
 */
@RestController
@RequestMapping("/tof")
public class TrueOrFalseController {

    @Autowired
    private TrueOrFalseService trueOrFalseService;

    @RequestMapping("/all")
    public List<TrueOrFalse> getAll(){
        return trueOrFalseService.selectAll();
    }


    @RequestMapping("/add")
    public ResponseEntity add(@RequestBody TrueOrFalse trueOrFalse){
        trueOrFalse.setCreateDate(TimeUtils.getNowTime());
        int res = trueOrFalseService.insert(trueOrFalse);
        if(res!=1){
            return new ResponseEntity(500,"系统繁忙");
        }
        return ResponseEntity.SUCCESS_RESPONSE;
    }

    @GetMapping("/find/{id}")
    public TrueOrFalse find(@PathVariable("id") int id){
        return trueOrFalseService.selectByPrimaryKey(id);
    }

    @RequestMapping("/search")
    public List<TrueOrFalse> search(QuestionDto questionDto){
        TrueOrFalse trueOrFalse = new TrueOrFalse();
        BeanUtils.copyProperties(questionDto,trueOrFalse);
        return trueOrFalseService.selectByAll(trueOrFalse);
    }


    @PostMapping("/caps")
    public ResponseEntity conditionAndPageSearch(QuestionDto questionDto){
        TrueOrFalse trueOrFalse = new TrueOrFalse();
        BeanUtils.copyProperties(questionDto,trueOrFalse);
        PageHelper.startPage(questionDto.getPageNum(),questionDto.getPageSize());
        List<TrueOrFalse> list =  trueOrFalseService.selectByAll(trueOrFalse);
        PageInfo pageInfo = new PageInfo(list,10);
        return new ResponseEntity(200,"success",pageInfo);
    }

    @RequestMapping("/tof/update")
    public ResponseEntity update(@RequestBody TrueOrFalse trueOrFalse){
        int res = trueOrFalseService.updateByPrimaryKeySelective(trueOrFalse);
        if(res!=1){
            return new ResponseEntity(500,"系统繁忙");
        }
        return ResponseEntity.SUCCESS_RESPONSE;
    }

    @RequestMapping("/tof/delete")
    public ResponseEntity delete(int id){
        int res = trueOrFalseService.deleteByPrimaryKey(id);
        if(res!=1){
            return new ResponseEntity(500,"出现异常");
        }
        return new ResponseEntity(200,"success");
    }

}
